package study.tabledriven;

/**
 * 这个是表驱动模式实现的保险费率获取
 */
public class Premium {


        private static final double[] RATES = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};

        // 注意：Java 中枚举类型不能直接作为数组元素，需要转换为整型或字符串
        private static final int[][] PREMIUM_RATE_CONDITIONS = {
                {Age.UNADULT.ordinal(), Gender.FEMALE.ordinal(), MaritalStatus.UNMARRIED.ordinal()},
                {Age.UNADULT.ordinal(), Gender.FEMALE.ordinal(), MaritalStatus.MARRIED.ordinal()},
                {Age.UNADULT.ordinal(), Gender.MALE.ordinal(), MaritalStatus.UNMARRIED.ordinal()},
                {Age.UNADULT.ordinal(), Gender.MALE.ordinal(), MaritalStatus.MARRIED.ordinal()},
                {Age.ADULT.ordinal(), Gender.FEMALE.ordinal(), MaritalStatus.UNMARRIED.ordinal()},
                {Age.ADULT.ordinal(), Gender.FEMALE.ordinal(), MaritalStatus.MARRIED.ordinal()},
                {Age.ADULT.ordinal(), Gender.MALE.ordinal(), MaritalStatus.UNMARRIED.ordinal()},
                {Age.ADULT.ordinal(), Gender.MALE.ordinal(), MaritalStatus.MARRIED.ordinal()}
        };

        private Premium() {}

        public static double getRate(int age, MaritalStatus maritalStatus, Gender gender) {
            Age ageCategory = getAgeCategory(age);
            int index = -1;

            for (int i = 0; i < PREMIUM_RATE_CONDITIONS.length; i++) {
                if (PREMIUM_RATE_CONDITIONS[i][0] == ageCategory.ordinal()
                        && PREMIUM_RATE_CONDITIONS[i][1] == gender.ordinal()
                        && PREMIUM_RATE_CONDITIONS[i][2] == maritalStatus.ordinal()) {
                    index = i;
                    break;
                }
            }

            if (index != -1) {
                return RATES[index];
            } else {
                throw new IllegalArgumentException("No rate found for the given conditions.");
            }
        }

        private static Age getAgeCategory(int age) {
            return age < 18 ? Age.UNADULT : Age.ADULT;
        }

        public static void main(String[] args) {
            // 示例：获取保费率
            double rate = Premium.getRate(25, MaritalStatus.UNMARRIED, Gender.MALE);
            System.out.println("Rate: " + rate);
        }

}


enum Age {
    UNADULT,
    ADULT
}

enum Gender {
    FEMALE,
    MALE
}

enum MaritalStatus {
    UNMARRIED,
    MARRIED
}